package com.example.courseapp.activity;

import static com.example.courseapp.utils.TimeUtils.getSafeDateFormat;
import static com.example.courseapp.utils.TimeUtils.isToday;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.courseapp.R;
import com.example.courseapp.RetrofitHelper;
import com.example.courseapp.adapter.SportRecordAdapter;
import com.example.courseapp.bean.ChildSportRecords;
import com.example.courseapp.bean.SportInfo;
import com.example.courseapp.net.CourseService;
import com.example.courseapp.utils.BigDecimalUtils;
import com.example.courseapp.utils.RxUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChildSportActivity extends AppCompatActivity {

    Button back, foodTuijian;

    RecyclerView recyclerView;

    CourseService service;
    Integer childID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_child_fodd);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        service = RetrofitHelper.getInstance().createService(CourseService.class);

         childID = getIntent().getIntExtra("childID", 0);

        SportRecordAdapter foodRecordAdapter = new SportRecordAdapter();

        back = findViewById(R.id.back);

        back.setOnClickListener(v -> {
            finish();
        });
        foodTuijian = findViewById(R.id.foodTuijian);
        foodTuijian.setText("运动推荐");
        foodTuijian.setOnClickListener(v -> {
            if (foodRecordAdapter.getData() != null && foodRecordAdapter.getData().size() > 0) {
                RxUtil.observe(Schedulers.newThread(), Observable.create(e -> {
                    double time = 0;
                    for (ChildSportRecords d : foodRecordAdapter.getData()) {
                        for (SportInfo s : d.newSport) {
                            String t = s.number > 1 ? ((BigDecimalUtils.div(String.valueOf(30 * s.number), "60", 1)) + "") : "0.5";
                            time += Double.valueOf(t);
                        }
                    }
                    e.onNext(time);
                    e.onComplete();
                })).subscribe(o -> {

                    Intent intent =new Intent(this, SportRecordActivity.class);
                    intent.putExtra("childID", childID);
                    intent.putExtra("time",(Double) o);
                    startActivity(intent);
                });
            }
        });

        recyclerView = findViewById(R.id.recycler);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(foodRecordAdapter);

        service.getSportList(childID).enqueue(new Callback<List<ChildSportRecords>>() {
            @Override
            public void onResponse(Call<List<ChildSportRecords>> call, Response<List<ChildSportRecords>> response) {
             /*   List<ChildSportRecords> data = response.body();
                if (data != null && data.size() > 0) {
                    List<ChildSportRecords> newData = new ArrayList<>();
                    for (ChildSportRecords records : data) {
                        boolean isToady = isToday(records.getCreateTime(), getSafeDateFormat("yyyy-MM-dd HH:mm:ss"));
                        if (isToady) {
                            newData.add(records);
                        }
                    }
                    foodRecordAdapter.bindData(newData);
                }*/

                List<ChildSportRecords> data = response.body();
                if (data != null && data.size() > 0) {
                    RxUtil.observe(Schedulers.newThread(), Observable.create(e -> {
                        List<ChildSportRecords> xData = new ArrayList<>();
                        for (int x = 0; x < data.size(); x++) {
                            ChildSportRecords records = data.get(x);
                            boolean isToady = isToday(records.getCreateTime(), getSafeDateFormat("yyyy-MM-dd HH:mm:ss"));
                            if (isToady) {
                                xData.add(records);
                            }
                        }
                        for (int x = 0; x < xData.size(); x++) {
                            ChildSportRecords records = xData.get(x);
                            List<SportInfo> newData = new ArrayList<>();
                            List<SportInfo> infoList = new Gson().fromJson(records.getSport(), new TypeToken<List<SportInfo>>() {
                            }.getType());
                            for (int i = 0; i < infoList.size(); i++) {
                                boolean isExist = false;
                                for (int j = 0; j < newData.size(); j++) {
                                    if (newData.get(j).getId() == infoList.get(i).getId()) {
                                        isExist = true;
                                        break;
                                    }
                                }
                                if (!isExist)
                                    newData.add(infoList.get(i));
                            }
                            records.newSport = newData;
                        }
                        e.onNext(xData);
                        e.onComplete();
                    })).subscribe(o1 -> {
                        foodRecordAdapter.bindData((List<ChildSportRecords>) o1);
                    });
                }
            }

            @Override
            public void onFailure(Call<List<ChildSportRecords>> call, Throwable t) {

            }
        });

    }
}